﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.IO;
using System.Xml;

namespace AJRavindiran.Jolt.Utilities.Parsing
{
    /// <summary>
    /// Represents a XML reader. Used to read XML data.
    /// </summary>
    public class XML
    {
        #region Fields
        /// <summary>
        /// Storage for all the XML data.
        /// </summary>
        private static XmlNodeList mXMLData;
        #endregion

        #region Properties
        /// <summary>
        /// Grabs a value correspondant of the key.
        /// </summary>
        /// <param name="file">The file to look for.</param>
        /// <param name="key">The key to look for in the file.</param>
        /// <returns>Returns the key's value.</returns>
        public string this[string file, string key]
        {
            get 
            {
                string value;
                try
                {
                    value = ReadValue(file, key);
                }
                catch (Exception ex) 
                { 
                    JoltEnvironment.GetLogger().WriteException(ex);
                    return "null";
                }
                return value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Grabs a value correspondant of the key.
        /// </summary>
        /// <param name="file">The file to look for.</param>
        /// <param name="key">The key to look for in the file.</param>
        /// <returns>Returns the key's value.</returns>
        public static string ReadValue(string file, string key)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(@".\Bin\" + file + ".xml");
                mXMLData = xDoc.GetElementsByTagName(key);
            }
            catch (Exception ex)
            {
                JoltEnvironment.GetLogger().WriteError(ex.ToString());
            }
            return mXMLData[0].InnerText;
        }
        #endregion
    }
}
